home *** CD-ROM | disk | FTP | other *** search
/ Night Owl 6 / Night Owl's Shareware - PDSI-006 - Night Owl Corp (1990).iso / 007a / cug315.zip / MOUSELIB.C < prev    next >
C/C++ Source or Header  |  1990-05-16  |  6KB  |  254 lines

  1.  
  2. /* mouses() functions changed to cmousel() in conformity with new */
  3. /* Microsoft mouse driver, dated 1-23-89.  Change by T. Clune, 6/89. */
  4. /* mouse_gets() added to check for mouse button termination of an */
  5. /* input sequence and inpause() added for mouse or key pause by T. Clune */
  6.  
  7. /* mouse_clear() and mouse_response() added 3/89 by T. Clune.  Unlike */
  8. /* the other functions in MOUSELIB, mouse_clear() & mouse_response() */
  9. /* are not just repackaged Microsoft mouse library basic functions. */
  10.  
  11. /* button_clicks() added 11/88 by T. Clune */
  12.  
  13. /* mouselib.c is a series of three functions for using the microsoft */
  14. /* mouse with the microsoft-supplied mouse.lib.  Written by T. Clune, 12/87 */
  15. /* Primary Documentation: _Microsoft Mouse User's Guide_, (subtitle: for IBM */
  16. /* Personal Computers) p. 175 ff. May be out of print, as the new mice don't */
  17. /* seem to include a copy of this book. The book is by Microsoft, copyright */
  18. /* 1984 and 85, no edition given.  Document Number: 990973001-000-R00-0785. */
  19.  
  20.  
  21. #include <stdio.h>  /* putc() macro def. */
  22. #include <conio.h>  /* header for kbhit() and getche() */
  23. #include "mouselib.h"
  24. #include "keys.h"
  25. #include "menu.h"   /* get_mouse_flag() header */
  26.  
  27. extern int cmousel(); /* the Microsoft mouse.lib large-model C func */
  28.  
  29.  
  30. /* button_clicks() reads the number of button clicks for BUTTON */
  31. /* since the last call to button_clicks(BUTTON).  Added 11/88 by T. Clune */
  32. /* NOTE WELL: BUTTON is normalized to the LEFT_BUTTON, RIGHT_BUTTON definitions */
  33. /* even though Microsoft uses different values for this function and the */
  34. /* button_status() function below. */
  35. /* The returned data is: b.opcode= current status of button (0=UP, 1=PRESSED) */
  36. /* b.status = number of clicks since last call to function */
  37. /* b.dx, b.dy=mouse position (virtual screen coords) at last button press */
  38.  
  39. mstruc button_clicks(button)
  40. int button;
  41. {
  42.     mstruc b;
  43.     b.opcode=BUTTON_CLICKS;
  44.     if(button==LEFT_BUTTON)
  45.     b.status=0;
  46.     if(button==RIGHT_BUTTON)
  47.     b.status=1;
  48.     if((button != LEFT_BUTTON) && (button != RIGHT_BUTTON))
  49.     b.status= -1;
  50.     else
  51.     cmousel(&b.opcode, &b.status, &b.dx, &b.dy);
  52.     return b;
  53.  
  54. }
  55.  
  56.  
  57.  
  58. /* button_read().status returns current condition of the mouse buttons */
  59. /* the .dx and .dy struct elements are position (NOT CHANGE IN POSITION) of */
  60. /* the "cursor" in virtual coords */
  61.  
  62. mstruc button_read()
  63. {
  64.     mstruc b;
  65.  
  66.     b.opcode=BUTTON_STATUS;
  67.     cmousel(&b.opcode, &b.status, &b.dx, &b.dy);
  68.     return b;
  69. }
  70.  
  71.  
  72.  
  73. /* inpause() waits for a keypress or a  mouse button press, then returns */
  74. /* the key if a keyboard entry was made, or <CR> if RIGHT mouse button was */
  75. /* pressed, ASCII 127 for any other button.  The value to return with a */
  76. /* mouse button press was a hard choice for me.  A '\0' is used for */
  77. /* function keys and alternate keys like the cursor keys as the first */
  78. /* of two arguments.  An '\n' is not normally returned by the keyboard, */
  79. /* because keyboards are by default opened as binary devices.  So <CR> */
  80. /* seemed the least objectionable value to use. 127 just seemed like a */
  81. /* safe no-op. inpause() is a substitute for getch(). */
  82.  
  83. int inpause()
  84. {
  85.     int c=LEFT_BUTTON_IN;  /* default for LEFT BUTTON or BOTH BUTTONS */
  86.     mstruc m;
  87.  
  88.     clear_input_devices();
  89.     for(;;)
  90.     {
  91.     if(kbhit())
  92.     {
  93.         c=getch();
  94.         break;
  95.     }
  96.     m=button_read();
  97.     if(m.status)    /* any button press will force a return */
  98.     {
  99.         if(m.status==RIGHT_BUTTON)
  100.         c=CARRIAGE_RETURN;
  101.         break;
  102.     }
  103.     }
  104.  
  105.     return c;
  106.  
  107. }
  108.  
  109.  
  110.  
  111.  
  112.  
  113.  
  114.  
  115.  
  116. /* m_install().status verifies that mouse and driver installed, and resets */
  117. /* mouse.  If m_install().status==0, not installed: if -1, installed. */
  118.  
  119. mstruc m_install()
  120. {
  121.  
  122.     mstruc m;
  123.     m.opcode=MOUSE_CHECK;
  124.  
  125.     cmousel(&m.opcode, &m.status, &m.dx, &m.dy);
  126.     if(!m.opcode)
  127.     printf("no mouse found\n");
  128.     return m;
  129.  
  130. }
  131.  
  132.  
  133.  
  134.  
  135.  
  136. /* mdpos_read().dx and .dy are the change in x and y mouse position */
  137. /* (unitless, -32K to +32K) since the last mdpos_read() function call */
  138.  
  139. mstruc mdpos_read()
  140. {
  141.  
  142.     mstruc m;
  143.  
  144.     m.opcode=POS_CHANGE;
  145.  
  146.     cmousel(&m.opcode, &m.status, &m.dx, &m.dy);
  147.     return m;
  148.  
  149. }
  150.  
  151.  
  152.  
  153. /* mouse_clear() clears the dx, dy registers of mdpos_read() and clears */
  154. /* the button_clicks() LEFT and RIGHT value.  If either mouse button is being */
  155. /* held down during mouse_clear() call, routine pauses for up to 1.0 sec and */
  156. /* then beeps to let you know that the buttons must be released. */
  157.  
  158. void mouse_clear()
  159. {
  160.  
  161.     int i=0;
  162.  
  163.     mdpos_read();
  164.     do
  165.     {
  166.     pause(0.1);
  167.     i++;
  168.     }while(button_read().status && i<10);
  169.  
  170.     if(i>=10)
  171.     while(button_read().status)
  172.         sound(1200, 0.1);
  173.  
  174.     button_clicks(LEFT_BUTTON);
  175.     button_clicks(RIGHT_BUTTON);
  176.  
  177.  
  178. }
  179.  
  180.  
  181.  
  182.  
  183.  
  184. /* mouse_gets() is a substitute for gets(), in which RIGHT_BUTTON is able */
  185. /* to be detected as a carriage return.  Like gets(), the string that */
  186. /* is returned is null-terminated. */
  187.  
  188. char * mouse_gets(sptr)
  189. char *sptr;
  190. {
  191.     int i=0;
  192.     unsigned char c='\0';
  193.  
  194.     mouse_clear();
  195.     while(c != CARRIAGE_RETURN)
  196.     {
  197.     if(button_read().status==RIGHT_BUTTON)
  198.         c = CARRIAGE_RETURN;
  199.     if(kbhit())
  200.     {
  201.         c=getche();
  202.         if(c==BACKSPACE)
  203.         {
  204.         putc(' ', stdout);
  205.         putc(BACKSPACE,stdout);
  206.         i--;
  207.         if(i<0)
  208.             i=0;
  209.         sptr[i]='\0';
  210.         }
  211.         else
  212.         {
  213.         if(c != CARRIAGE_RETURN)
  214.         {
  215.             sptr[i]=c;
  216.             i++;
  217.         }
  218.         }
  219.     }
  220.     }
  221.     sptr[i]='\0';
  222.     printf("\n");
  223.  
  224.     return sptr;
  225.  
  226. }
  227.  
  228.  
  229.  
  230.  
  231. /* mouse_response() checks for movement in the mouse or button-press activity */
  232. /* It returns the MOTION_FACTOR-scaled dx & dy and either LEFT_BUTTON, */
  233. /* RIGHT_BUTTON, BOTH_BUTTONS, or 0 for the .status struct element, depending */
  234. /* on the button activity.  DOES NOT RETURN CLICK COUNT, only yes/no */
  235.  
  236. mstruc mouse_response()
  237. {
  238.     mstruc ret_val;
  239.  
  240.     ret_val=mdpos_read();
  241.     ret_val.dx /=MOTION_FACTOR;
  242.     ret_val.dy /=MOTION_FACTOR;
  243.  
  244.     ret_val.status=0;
  245.     if(button_clicks(LEFT_BUTTON).status)
  246.     ret_val.status = LEFT_BUTTON;
  247.     if(button_clicks(RIGHT_BUTTON).status)
  248.     ret_val.status +=RIGHT_BUTTON;
  249.  
  250.     return ret_val;
  251.  
  252. }
  253.  
  254.